﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CooStationsToolsNew
{
    /// <summary>
    /// WorkMainAreaUC.xaml 的交互逻辑
    /// </summary>
    public partial class WorkMainAreaUC : UserControl
    {
        Window win;
        public static Image divImg = null;
        public static Canvas workcanvas = null;
        PageTools paget = new PageTools();
        ProjectTools pt = new ProjectTools();
        public static AdornerLayer layer;
        public static MyCanvasAdorner myCan;
        public static double _newx, _newy, _oldx, _oldy;
        public WorkMainAreaUC(Window _win)
        {
            InitializeComponent();
            win=_win;
            dizuoimg.Visibility = Visibility.Collapsed;
        }
        public void InitWorkMainArea()
        {
            bodySlider.Value = 1;
            workcanvas = CreateCanvas();
            pt.InitTheWork(App.dt, workcanvas);
            WorkAreaBorder.Child = workcanvas;
            bodySlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(bodySlider_ValueChanged);
            bodySlider.Value = 0.5;
          //  mainAreaBorder.Width = mainAreaBorder.ActualWidth;
          //  mainAreaBorder.Height = mainAreaBorder.ActualHeight;
            worklogo.ImageSource = null;
        } 

        void bodySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            bodystxt.Text = Convert.ToInt32((bodySlider.Value * 100)) + "%";
            viewbox.Width = bodySlider.Value * workcanvas.Width;
            viewbox.Height = bodySlider.Value * workcanvas.Height;
            mainAreaBorder.Width = viewbox.Width +800;
            mainAreaBorder.Height = viewbox.Height+800;
            scrollViewer4.ScrollToHorizontalOffset(scrollViewer4.ScrollableWidth/2);
            scrollViewer4.ScrollToVerticalOffset(scrollViewer4.ScrollableHeight/2);
        }

        /// <summary>
        /// 初始化工作区
        /// </summary>
        /// <returns></returns>
        private Canvas CreateCanvas()
        {
           Canvas canvas = new Canvas();
            canvas.Name = "work";
            canvas.MouseMove += new MouseEventHandler(canvas_MouseMove);
            canvas.MouseDown += new MouseButtonEventHandler(canvas_MouseDown);
            canvas.MouseUp += new MouseButtonEventHandler(canvas_MouseUp);
            canvas.Background = new SolidColorBrush(Colors.White);
            canvas.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            canvas.VerticalAlignment = System.Windows.VerticalAlignment.Center; ;
            canvas.Width = double.Parse(App.proWidth);
            canvas.Height = double.Parse(App.proHeight);
            return canvas;
        }

        void canvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (App.uiobj != null)
            {
                if (App.uiobj.GetType() == typeof(Image))
                {
                    Image img = App.uiobj as Image;
                  
                }
            }
        }
        ModelTools model = new ModelTools();
        void canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {          
            ////用于获得工作区边界
            //MainWindow.GetSplitterPoint();
            if (App.uiobj != null)
            {
                
                if (App.uiobj.GetType() == typeof(Image))
                {
                    Image img = App.uiobj as Image;
                   // ModelTools.autoRTB.MoveFocus(); = false;
                  //  ModelTools.autoRTB.IsEnabled = false;
                    Keyboard.ClearFocus();
                    Canvas pcan = img.Parent as Canvas;
                    Point point = e.MouseDevice.GetPosition(pcan);
                    Point pointimg = img.TranslatePoint(new Point(0, 0), pcan);
                    _newx = point.X;
                    _newy = point.Y;
                    _oldx = pointimg.X;
                    _oldy = pointimg.Y;
                }
                else if (App.uiobj.GetType() == typeof(Rectangle))
                {
                    Rectangle rect = App.uiobj as Rectangle;
                    Canvas pcan = rect.Parent as Canvas;
                  
                    Point point = e.MouseDevice.GetPosition(pcan);
                    Point pointimg = rect.TranslatePoint(new Point(0, 0), pcan);
                    _newx = point.X;
                    _newy = point.Y;
                    _oldx = pointimg.X;
                    _oldy = pointimg.Y;
                }
                else if (App.uiobj.GetType() == typeof(TextBlock))
                {
                    TextBlock textb = App.uiobj as TextBlock;
                    Canvas pcan = textb.Parent as Canvas;
                    layer = AdornerLayer.GetAdornerLayer(pcan);
                   
                    Point point = e.MouseDevice.GetPosition(pcan);
                    Point pointimg = textb.TranslatePoint(new Point(0, 0), pcan);
                    _newx = point.X;
                    _newy = point.Y;
                    _oldx = pointimg.X;
                    _oldy = pointimg.Y;
                }
                else if (App.uiobj.GetType() == typeof(Canvas))
                {
                   
                        Canvas div = App.uiobj as Canvas;
                        Canvas pcan = div.Parent as Canvas;
                        layer = AdornerLayer.GetAdornerLayer(pcan);
                       
                        Point point = e.MouseDevice.GetPosition(pcan);
                        Point pointimg = div.TranslatePoint(new Point(0, 0), pcan);
                        _newx = point.X;
                        _newy = point.Y;
                        _oldx = pointimg.X;
                        _oldy = pointimg.Y;
                    }
                
            }
        
        }
        public void RemoveAllAdorner()
        {
            if (myCan != null)
            {
                layer.Remove(myCan);
            }
        }
        /// <summary>
        /// 删除工作区
        /// </summary>
        public void DelCanvas()
        {
            if (workcanvas != null)
            {
                workcanvas.Children.Clear();
            }
            WorkAreaBorder.Child = null;
            workcanvas = null;
            bodySlider.ValueChanged -= new RoutedPropertyChangedEventHandler<double>(bodySlider_ValueChanged);
            bodySlider.Value = 1;
        }
        void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (App.uiobj != null)
                {

                    if (App.uiobj.GetType() == typeof(Image))
                    {
                        Image img = App.uiobj as Image;
                        Canvas can = img.Parent as Canvas;
                        if (e.LeftButton == MouseButtonState.Pressed)
                        {
                            //移动时 让移动对象透明度变成0.5
                            
                            Point mp = e.MouseDevice.GetPosition(can);
                            double nx = mp.X - _newx;
                            double ny = mp.Y - _newy;
                            _newx = mp.X;
                            _newy = mp.Y;
                            img.SetValue(Canvas.LeftProperty, double.Parse((_oldx + nx).ToString("F0")));
                            img.SetValue(Canvas.TopProperty, double.Parse((_oldy + ny).ToString("F0")));
                            _oldx += nx;
                            _oldy += ny;
                            ////用于移动到边界增加边界量
                            //Point winp = Mouse.GetPosition(win);
                            //if (winp.X <= MainWindow.leftpt.X+10)
                            //{
                            //    mainAreaBorder.Width += 2;
                            //    //MessageBox.Show("fdsafdsa");
                            //}
                        }
                    }
                    else if (App.uiobj.GetType() == typeof(Rectangle))
                    {
                        Rectangle rect = App.uiobj as Rectangle;
                        Canvas can = rect.Parent as Canvas;
                        if (e.LeftButton == MouseButtonState.Pressed)
                        {
                            //移动时 让移动对象透明度变成0.5
                            
                            Point mp = e.MouseDevice.GetPosition(can);
                            double nx = mp.X - _newx;
                            double ny = mp.Y - _newy;
                            _newx = mp.X;
                            _newy = mp.Y;
                            rect.SetValue(Canvas.LeftProperty, double.Parse((_oldx + nx).ToString("F0")));
                            rect.SetValue(Canvas.TopProperty, double.Parse((_oldy + ny).ToString("F0")));
                            _oldx += nx;
                            _oldy += ny;
                            ////用于移动到边界增加边界量
                            //Point winp = Mouse.GetPosition(win);
                            //if (winp.X <= MainWindow.leftpt.X+10)
                            //{
                            //    mainAreaBorder.Width += 2;
                            //    //MessageBox.Show("fdsafdsa");
                            //}
                        }
                    }
                    else if (App.uiobj.GetType() == typeof(TextBlock))
                    {
                        TextBlock textb = App.uiobj as TextBlock;
                        Canvas can = textb.Parent as Canvas;
                        if (e.LeftButton == MouseButtonState.Pressed)
                        {
                            //移动时 让移动对象透明度变成0.5
                           
                            Point mp = e.MouseDevice.GetPosition(can);
                            double nx = mp.X - _newx;
                            double ny = mp.Y - _newy;
                            _newx = mp.X;
                            _newy = mp.Y;
                            textb.SetValue(Canvas.LeftProperty, double.Parse((_oldx + nx).ToString("F0")));
                            textb.SetValue(Canvas.TopProperty, double.Parse((_oldy + ny).ToString("F0")));
                            _oldx += nx;
                            _oldy += ny;
                            ////用于移动到边界增加边界量
                            //Point winp = Mouse.GetPosition(win);
                            //if (winp.X <= MainWindow.leftpt.X+10)
                            //{
                            //    mainAreaBorder.Width += 2;
                            //    //MessageBox.Show("fdsafdsa");
                            //}
                        }
                    }
                    else if (App.uiobj.GetType() == typeof(Canvas))
                    {
                        Canvas div = App.uiobj as Canvas;
                        Canvas can = div.Parent as Canvas;
                        if (e.LeftButton == MouseButtonState.Pressed)
                        {
                            //移动时 让移动对象透明度变成0.5
                          
                            Point mp = e.MouseDevice.GetPosition(can);
                            double nx = mp.X - _newx;
                            double ny = mp.Y - _newy;
                            _newx = mp.X;
                            _newy = mp.Y;
                            div.SetValue(Canvas.LeftProperty, double.Parse((_oldx + nx).ToString("F0")));
                            div.SetValue(Canvas.TopProperty, double.Parse((_oldy + ny).ToString("F0")));
                            _oldx += nx;
                            _oldy += ny;
                            ////用于移动到边界增加边界量
                            //Point winp = Mouse.GetPosition(win);
                            //if (winp.X <= MainWindow.leftpt.X+10)
                            //{
                            //    mainAreaBorder.Width += 2;
                            //    //MessageBox.Show("fdsafdsa");
                            //}
                        }
                    }
                }
            }
        }

        private void bodystxt_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void wsamll_Click(object sender, RoutedEventArgs e)
        {
            bodySlider.Value = bodySlider.Value - bodySlider.TickFrequency;
        }

        private void wbig_Click(object sender, RoutedEventArgs e)
        {
            bodySlider.Value = bodySlider.Value + bodySlider.TickFrequency;
        }

        private void mainAreaBorder_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void adaptiveBtn_Click(object sender, RoutedEventArgs e)
        {

        }
       
        public void ToNewPage()
        {
            //增加一个新页
            paget.AddPage(App.cpage);
            MainWindow mw=win as MainWindow;
            mw.InitNewPage(App.cpage.PageName);
        }
    }
}
